package com.hzo.great.leisuregreat.utils;

import android.util.Log;

import java.util.Map;

/**
 *  LogUtils
 *  @author QunWang
 *  created at 2018/6/25 14:40
 */
public class LogUtils {

    //是否打印 测试 log
    private static final boolean IS_LOG_OPEN = true;

    private static final String TAG = "com.dm.ox.log";
    private static String volleyRequest = "volley_request";

    public static void d(String msg) {
        if (IS_LOG_OPEN) {
            Log.d(TAG, msg);
        }
    }

    public static void i(String msg) {
        if (IS_LOG_OPEN) {
            Log.i(TAG, msg);
        }
    }

    public static void i(String tag, String msg) {
        if (IS_LOG_OPEN) {
            Log.i(tag, msg);
        }
    }

    public static void w(String msg) {
        if (IS_LOG_OPEN) {
            Log.w(TAG, msg);
        }
    }

    public static void e(String msg) {
        if (IS_LOG_OPEN) {
            Log.e(TAG, msg);
        }
    }

    public static void printMap(String str, Map<String, String> map) {
        StringBuilder sb = new StringBuilder();
        if (str != null) {
            sb.append("\n").append(str).append(":\n");
        }
        for (Map.Entry<String, String> entry :
                map.entrySet()) {
            sb.append(entry.getKey()).append(":").append(entry.getValue()).append("\n");
        }
        i(volleyRequest, sb.toString());
    }

    public static void printRequest(Map<String, String> header, Map<String, String> map, String where, String json) {
        StringBuilder sb = new StringBuilder();
        sb.append("---> POST :").append(where).append("\n");
        if (header != null) {
            for (Map.Entry<String, String> entry :
                    header.entrySet()) {
                sb.append(entry.getKey()).append(":").append(entry.getValue()).append("\n ");
            }
        }
        sb.append("---> params : \n");
        if (map != null) {
            for (Map.Entry<String, String> entry :
                    map.entrySet()) {
                sb.append(entry.getKey()).append(":").append(entry.getValue()).append("\n");
            }
        }
        sb.append("---> json : \n").append(json);
        show(sb.toString());
    }

    public static void printRequest(Map<String, String> header, String where, Exception e) {
        StringBuilder sb = new StringBuilder();
        sb.append("---> POST :").append(where).append("\n");
        if (header != null) {
            for (Map.Entry<String, String> entry :
                    header.entrySet()) {
                sb.append(entry.getKey()).append(":").append(entry.getValue()).append("\n ");
            }
        }
//        sb.append("---> params : \n");
//        for (Map.Entry<String, String> entry :
//                map.entrySet()) {
//            sb.append(entry.getKey() + ":" + entry.getValue() + "\n");
//        }
        sb.append("---> request error : \n").append(e.getMessage());
        show(sb.toString());
    }

    public static void printErrorRequest(Map<String, String> header, String where, String message) {
        StringBuilder sb = new StringBuilder();
        sb.append("---> POST :").append(where).append("\n");
        if (header != null) {
            for (Map.Entry<String, String> entry :
                    header.entrySet()) {
                sb.append(entry.getKey()).append(":").append(entry.getValue()).append("\n ");
            }
        }
        sb.append("---> exception : \n").append(message);
        show(sb.toString());
    }

    // 使用Log来显示调试信息,因为log在实现上每个message有4k字符长度限制
    // 所以这里使用自己分节的方式来输出足够长度的message
    private static void show(String str) {
        str = str.trim();
        int index = 0;
        int maxLength = 3500;
        String sub;
        while (index < str.length()) {
            // java的字符不允许指定超过总的长度end

            //str.length() = 456,  index = 0, maxLength = 4000
            //当总长度小于   起始点加最大长度  从begin(index)打印到最后
            //当总长度大于   起始点加最大长度  从begin(index)打印到index+maxLength

            if (str.length() <= index + maxLength) {//当总长度小于或等于起始点加上最大点
                sub = str.substring(index);
            } else {//当总长度大于起始点加上最大点
                sub = str.substring(index, index + maxLength);
            }

            index += maxLength;
            i(volleyRequest, sub.trim());
        }
    }
}
